package thread;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TimeHandler implements InvocationHandler {
    private Object target;

    public TimeHandler(Object t) {
        this.target = t;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("当前正在执行:" + method.getName());
        if(method.getName().equals("put")){
            if(args[0].equals("法轮功")||args[1].equals("法轮功"))
                return null;
        }

        long start = System.nanoTime();
        Object o = method.invoke(target, args);//真的干活
        long end = System.nanoTime();
        System.out.println(method.getName() + "执行花费了:" + (end - start) + "纳秒");
        return o;
    }

    public Object getProxy() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
}
